﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;

namespace SDLITS.Web.Personal
{
    public partial class AddressBookManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("AddressBookChange.aspx", "新增通讯录");
                BindGrid(GetWhere());
            }
        }
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_AddressbookUser>().Where(wc).Count();
            List<V_AddressbookUser> items = SDDbSession.Default.From<V_AddressbookUser>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }
        protected WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && V_AddressbookUser._.Name.Contain(ttbxMyBox2.Text.Trim());
            }
            if (drp_atype.SelectedValue != "")
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                if (drp_atype.SelectedValue == "1")
                {
                    wc = wc && V_AddressbookUser._.StatusID == 1;
                }
                else if (drp_atype.SelectedValue == "2")
                {
                    if (string.IsNullOrEmpty(user.CompanyID.ToString()) || user.CompanyID==-1)
                    {
                        wc = wc && V_AddressbookUser._.StatusID == 2 && V_AddressbookUser._.CompanyID.IsNull();
                    }
                    else
                    {
                        wc = wc && V_AddressbookUser._.StatusID == 2 && V_AddressbookUser._.CompanyID == user.CompanyID;
                    }
                }
                else
                {
                    wc = wc && V_AddressbookUser._.StatusID == drp_atype.SelectedValue && V_AddressbookUser._.CreateUserID == user.UserID;
                }
            }
            return wc;
        }
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                Grid1.PageIndex = 0;
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            Grid1.PageIndex = 0;
            BindGrid(GetWhere());
        }

        protected void Unnamed_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid(GetWhere());
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            e.Values[2] = e.Values[2].ToString() == "False" ? "<font color='green'>女</font>" : "<font color='green'>男</font>";
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                SDDbSession.Default.Delete<AddressBook>(AddressBook._.AddrID == id);
                BindGrid(GetWhere());

            }
        }
    }
}
